package cn.edu.sgu.www.mhxysy.service.exp.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.dto.exp.GradeExpDTO;
import cn.edu.sgu.www.mhxysy.entity.exp.ExpCategory;
import cn.edu.sgu.www.mhxysy.entity.exp.GradeExp;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.exp.ExpCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.exp.GradeExpMapper;
import cn.edu.sgu.www.mhxysy.pager.exp.GradeExpPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.exp.GradeExpService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class GradeExpServiceImpl implements GradeExpService {

    private final GradeExpMapper gradeExpMapper;
    private final ExpCategoryMapper expCategoryMapper;

    @Autowired
    public GradeExpServiceImpl(GradeExpMapper mapper, ExpCategoryMapper expCategoryMapper) {
        this.gradeExpMapper = mapper;
        this.expCategoryMapper = expCategoryMapper;
    }

    @Override
    public void insert(GradeExpDTO gradeExpDTO) {
        GradeExp gradeExp = toEntity(gradeExpDTO);

        gradeExp.setId(null);

        gradeExpMapper.insert(gradeExp);
    }

    @Override
    public void updateById(GradeExpDTO gradeExpDTO) {
        if (gradeExpDTO.getId() == null) {
            throw new GlobalException(ResponseCode.BAD_REQUEST, "经验ID不能为空！");
        }

        GradeExp gradeExp = toEntity(gradeExpDTO);

        gradeExpMapper.updateById(gradeExp);
    }

    @Override
    public GradeExp selectRoleExp(Integer grade) {
        // 查询经验类型
        QueryWrapper<ExpCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("name", "角色经验");

        ExpCategory expCategory = expCategoryMapper.selectOne(queryWrapper);

        if (expCategory == null) {
            throw new GlobalException(ResponseCode.NOT_FOUND, "找不到角色经验的相关类型信息！");
        }

        QueryWrapper<GradeExp> queryWrapper1 = new QueryWrapper<>();

        queryWrapper1.eq("grade", grade);
        queryWrapper1.eq("category_id", expCategory.getId());

        return gradeExpMapper.selectOne(queryWrapper1);
    }

    @Override
    public GradeExp selectChongwuExp(Integer grade) {
        // 查询经验类型
        QueryWrapper<ExpCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("name", "宠物经验");

        ExpCategory expCategory = expCategoryMapper.selectOne(queryWrapper);

        if (expCategory == null) {
            throw new GlobalException(ResponseCode.NOT_FOUND, "找不到宠物经验的相关类型信息！");
        }

        UpdateWrapper<GradeExp> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("grade", grade);
        updateWrapper.eq("category_id", expCategory.getId());

        return gradeExpMapper.selectOne(updateWrapper);
    }

    @Override
    public Page<GradeExp> selectByPage(GradeExpPager pager) {
        QueryWrapper<GradeExp> queryWrapper = new QueryWrapper<>();
        Page<GradeExp> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getCategoryId() != null,
                "category_id", pager.getCategoryId()
        );

        return gradeExpMapper.selectPage(page, queryWrapper);
    }

    private GradeExp toEntity(GradeExpDTO gradeExpDTO) {
        GradeExp gradeExp = new GradeExp();

        gradeExp.setId(gradeExpDTO.getId());
        gradeExp.setExp(gradeExpDTO.getExp());
        gradeExp.setGrade(gradeExpDTO.getGrade());
        gradeExp.setCategoryId(gradeExpDTO.getCategoryId());

        return gradeExp;
    }

}